package com.how2java.tmall.controller;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpSession;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.how2java.tmall.pojo.Product;
import com.how2java.tmall.pojo.Productimage;
import com.how2java.tmall.service.ProductImageService;
import com.how2java.tmall.service.ProductService;
import com.how2java.tmall.util.ImageUtil;

@Controller
public class ProductImageController {
	@Autowired
	private ProductService productService;
	@Autowired
	private ProductImageService productImageService;

    @RequestMapping("admin_productImage_list")
    public String list(int pid, Model model) {
        Product p =productService.get(pid);
        List<Productimage> pisSingle = productImageService.list(pid, ProductImageService.type_single);
        List<Productimage> pisDetail = productImageService.list(pid, ProductImageService.type_detail);
 
        model.addAttribute("p", p);
        model.addAttribute("pisSingle", pisSingle);
        model.addAttribute("pisDetail", pisDetail);
 
        return "admin/listProductImage";
    }
	
    
    @RequestMapping("admin_productImage_add")
    public String add(Productimage  pi, HttpSession session, MultipartFile image) {
    	MultipartFile imageLocation = image;
        productImageService.add(pi);
        String fileName = pi.getId()+ ".jpg";
        String imageFolder;
        String imageFolder_small=null;
        String imageFolder_middle=null;
        String imageFolderLocation="";
        String imageFolderLocation_small="";
        String imageFolderLocation_middle="";
        if(ProductImageService.type_single.equals(pi.getType())){
        	/*---服务器路径---*/
            imageFolder= session.getServletContext().getRealPath("img/productSingle/");
            imageFolder_small= session.getServletContext().getRealPath("img/productSingle_small/");
            imageFolder_middle= session.getServletContext().getRealPath("img/productSingle_middle/");
            
            /*---本地路径,部署到服务器后需注释掉*/
            imageFolderLocation = "F:\\git\\Tmall_ssm\\tmall_ssm\\src\\main\\webapp\\img\\productSingle\\";
            imageFolderLocation_small = "F:\\git\\Tmall_ssm\\tmall_ssm\\src\\main\\webapp\\img\\productSingle_small\\";
            imageFolderLocation_middle = "F:\\git\\Tmall_ssm\\tmall_ssm\\src\\main\\webapp\\img\\productSingle_middle\\";
        }
        else{
            imageFolder= session.getServletContext().getRealPath("img/productDetail");
            
            imageFolderLocation = "F:\\git\\Tmall_ssm\\tmall_ssm\\src\\main\\webapp\\img\\productDetail";
            
        }
        /*---服务器端---*/
        InputStream is = null;
        OutputStream os = null;
        try {
        	os = new FileOutputStream(imageFolder+"\\"+fileName);
            is = imageLocation.getInputStream();
            IOUtils.copy(is, os);
 
            File f = new File(imageFolder, fileName);
            if(ProductImageService.type_single.equals(pi.getType())) {
                File f_small = new File(imageFolder_small, fileName);
                File f_middle = new File(imageFolder_middle, fileName);
 
                ImageUtil.resizeImage(f, 56, 56, f_small);
                ImageUtil.resizeImage(f, 217, 190, f_middle);
            }
            
            /*---本地端*/
            os = new FileOutputStream(imageFolderLocation+"\\"+fileName);
            is = imageLocation.getInputStream();
            IOUtils.copy(is, os);
            
            f = new File(imageFolderLocation, fileName);
 
            if(ProductImageService.type_single.equals(pi.getType())) {
                File f_small = new File(imageFolderLocation_small, fileName);
                File f_middle = new File(imageFolderLocation_middle, fileName);
 
                ImageUtil.resizeImage(f, 56, 56, f_small);
                ImageUtil.resizeImage(f, 217, 190, f_middle);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:admin_productImage_list?pid="+pi.getPid();
    }
    
    
    @RequestMapping("admin_productImage_delete")
    public String delete(int id,HttpSession session) {
        Productimage pi = productImageService.get(id);
     
        String fileName = pi.getId()+ ".jpg";
        String imageFolder;
        String imageFolder_small=null;
        String imageFolder_middle=null;
     
        if(ProductImageService.type_single.equals(pi.getType())){
            imageFolder= session.getServletContext().getRealPath("img/productSingle");
            imageFolder_small= session.getServletContext().getRealPath("img/productSingle_small");
            imageFolder_middle= session.getServletContext().getRealPath("img/productSingle_middle");
            File imageFile = new File(imageFolder,fileName);
            File f_small = new File(imageFolder_small,fileName);
            File f_middle = new File(imageFolder_middle,fileName);
            imageFile.delete();
            f_small.delete();
            f_middle.delete();
            
            String imageFolderLocation = "F:\\git\\Tmall_ssm\\tmall_ssm\\src\\main\\webapp\\img\\productSingle\\";
            String imageFolderLocation_small = "F:\\git\\Tmall_ssm\\tmall_ssm\\src\\main\\webapp\\img\\productSingle_small\\";
            String imageFolderLocation_middle = "F:\\git\\Tmall_ssm\\tmall_ssm\\src\\main\\webapp\\img\\productSingle_middle\\";
            imageFile = new File(imageFolderLocation,fileName);
            f_small = new File(imageFolderLocation_small,fileName);
            f_middle = new File(imageFolderLocation_middle,fileName);
            imageFile.delete();
            f_small.delete();
            f_middle.delete();
            
        }
        else{
            imageFolder= session.getServletContext().getRealPath("img/productDetail");
            File imageFile = new File(imageFolder,fileName);
            imageFile.delete();
            
            String imageFolderLocation = "F:\\git\\Tmall_ssm\\tmall_ssm\\src\\main\\webapp\\img\\productDetail\\";
            imageFile = new File(imageFolderLocation,fileName);
            imageFile.delete();
            
            
        }
     
        productImageService.delete(id);
     
        return "redirect:admin_productImage_list?pid="+pi.getPid();
    }
    
    
    
    
	
}
